<?php
namespace app\common\model;

use think\Model;

class ConfigModel extends Model
{
    protected $name = 'config';
    protected $autoWriteTimestamp = false;

    // 获取配置变量
    public function get_value($name)
    {
        return $this->getFieldByName($name, 'value');
    }

    // 保存或更新数据
    public function set_save($post, $type = '', $desc = '')
    {
        $update_data = [];
        $save_data   = [];
        foreach ($post as $k => $v) {
            $save['value'] = $v;
            $save['name']  = $k;
            $save['inc_type'] = $type;
            $save['desc'] = $desc;

            // 查询是否已经存在数据
            $id = $this->getFieldByName($save['name'], 'id');
            if (!empty($id)) {
                $update = $save;
                $update['id'] = $id;
                array_push($update_data, $update);
            } else {
                array_push($save_data, $save);
            }
        }

        $this->saveAll($update_data);
        $this->saveAll($save_data);
    }
}
